const app = getApp();
Page({
  data: {
    mdfl: [],
    bblx: ['基础版本'],
    id: '',
    joinString: '',
    uploaderList: [],
    uploaderLists: [],
    uploaderNum: 0,
    showUpload: true,
  },

  onLoad:function(e){
    this.data.store_id = e.store_id;
    this.getStoreInfo();
  },

  getStoreInfo: function(e) {
    var t = this
    app.visit("register/getStoreInfo", {
      store_id:t.data.store_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        c.data.info.sltj_zk = c.data.info.sltj_zk&&c.data.info.sltj_zk>0?c.data.info.sltj_zk:10;
        c.data.info.yd_price = c.data.info.yd_price?c.data.info.yd_price:0.01;
        t.setData({
          info:c.data.info,
          uploaderList:c.data.info.store_imgs?c.data.info.store_imgs:[],
          uploaderLists:c.data.info.store_img?c.data.info.store_img:[],
          mdfl:c.data.store_type
        })
        setTimeout(function () {
          var mdfl = t.data.mdfl
          var type_id = c.data.info.type_id
          for (let i = 0; i < mdfl.length; i++) {
            if (mdfl[i].id == type_id) {
              t.setData({
                mdfl_index: i
              })
            }
          }
        }, 1000) //延迟时间 这里是1秒
      }else{
        app.showToast(c.msg)
      }
    })
  },

  setTitle:function(e){
    var title = e.detail.value;
    var info = this.data.info;
    info.title = title;
    this.setData({
      info:info
    })
  },

  setStorePhone:function(e){
    var store_phone = e.detail.value;
    var info = this.data.info;
    info.store_phone = store_phone;
    this.setData({
      info:info
    })
  },

  setPerCapita:function(e){
    var per_capita = e.detail.value;
    var info = this.data.info;
    info.per_capita = per_capita;
    this.setData({
      info:info
    })
  },

  setYytime:function(e){
    var yytime = e.detail.value;
    var info = this.data.info;
    info.yytime = yytime;
    this.setData({
      info:info
    })
  },
  setSltjZk:function(e){
    var sltj_zk = e.detail.value;
    var info = this.data.info;
    info.sltj_zk = sltj_zk;
    this.setData({
      info:info
    })
  },
  setYdPrice:function(e){
    var yd_price = e.detail.value;
    var info = this.data.info;
    info.yd_price = yd_price;
    this.setData({
      info:info
    })
  },
  setDiyXfprice:function(e){
    var diy_xfprice = e.detail.value;
    var info = this.data.info;
    info.diy_xfprice = diy_xfprice;
    this.setData({
      info:info
    })
  },
  setGchbPrice:function(e){
    var gchb_price = e.detail.value;
    var info = this.data.info;
    info.gchb_price = gchb_price;
    this.setData({
      info:info
    })
  },
  setGchbEndPrice:function(e){
    var gchb_end_price = e.detail.value;
    var info = this.data.info;
    info.gchb_end_price = gchb_end_price;
    this.setData({
      info:info
    })
  },
  setDgchbPrice:function(e){
    var dgchb_price = e.detail.value;
    var info = this.data.info;
    info.dgchb_price = dgchb_price;
    this.setData({
      info:info
    })
  },
  setDgchbEndPrice:function(e){
    var dgchb_end_price = e.detail.value;
    var info = this.data.info;
    info.dgchb_end_price = dgchb_end_price;
    this.setData({
      info:info
    })
  },
  setFrName:function(e){
    var fr_name = e.detail.value;
    var info = this.data.info;
    info.fr_name = fr_name;
    this.setData({
      info:info
    })
  },
  setKaiHuhang:function(e){
    var kaihuhang = e.detail.value;
    var info = this.data.info;
    info.kaihuhang = kaihuhang;
    this.setData({
      info:info
    })
  },
  setTxZhanghu:function(e){
    var tx_zhanghu = e.detail.value;
    var info = this.data.info;
    info.tx_zhanghu = tx_zhanghu;
    this.setData({
      info:info
    })
  },
  setTxMobile:function(e){
    var tx_mobile = e.detail.value;
    var info = this.data.info;
    info.tx_mobile = tx_mobile;
    this.setData({
      info:info
    })
  },
  setZxPrice:function(e){
    var zx_price = e.detail.value;
    var info = this.data.info;
    info.zx_price = zx_price;
    this.setData({
      info:info
    })
  },
  setFzPrice:function(e){
    var fz_price = e.detail.value;
    var info = this.data.info;
    info.fz_price = fz_price;
    this.setData({
      info:info
    })
  },
  setYggzPrice:function(e){
    var yggz_price = e.detail.value;
    var info = this.data.info;
    info.yggz_price = yggz_price;
    this.setData({
      info:info
    })
  },
  setMaolilv:function(e){
    var maolilv = e.detail.value;
    var info = this.data.info;
    info.maolilv = maolilv;
    this.setData({
      info:info
    })
  },
  setDltishiyu:function(e){
    var dltishiyu = e.detail.value;
    var info = this.data.info;
    info.dltishiyu = dltishiyu;
    this.setData({
      info:info
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var mdfl = this.data.mdfl
    var type_id = mdfl[e.detail.value].id
    var info = this.data.info;
    info.type_id = type_id;
    this.setData({
      mdfl_index: e.detail.value,
      info:info
    })
  },
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? that.setData({
              uploaderList: that.data.uploaderList.concat(a.data.url),
              uploaderLists: that.data.uploaderLists.concat(a.data.img)
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },

  clearImg: function (e) {
    var that = this
    var nowList = []; 
    var nowLists = [];
    var uploaderList = that.data.uploaderList;
    var uploaderLists = that.data.uploaderLists;

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        var arr = that.data.joinString.split(',')
        arr.splice(i, 1); 
        var newArr = arr.join(',')
        that.setData({
          joinString: newArr,
          id: newArr + ','
        })
      } else {
        nowList.push(uploaderList[i])
        nowLists.push(uploaderLists[i])
      }
    }
    this.setData({
      uploaderList: nowList,
      uploaderLists: nowLists,
    })
  },
  bj_radio: function(e) {
    var info = this.data.info;
    info.is_baojian = e.detail.value;
    this.setData({
      info:info
    })
  },
  yz_radio: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var info = this.data.info;
    info.is_yzzk = e.detail.value
    this.setData({
      info:info
    })
  },
  fp_radio: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var info = this.data.info;
    info.is_jmfp = e.detail.value
    this.setData({
      info:info
    })
  },
  jb_radio: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var info = this.data.info;
    info.is_jbdxj = e.detail.value
    this.setData({
      info:info
    })
  },
  sl_radio: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var info = this.data.info;
    info.is_sltj = e.detail.value
    this.setData({
      info:info
    })
  },
  tijiao:function(e){
    var t = this
    var store_id = t.data.store_id;
    app.visit("register/wanShanStoreInfo", {
      store_id: store_id,
      info:JSON.stringify(t.data.info),
      uploaderLists:t.data.uploaderLists
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        wx.navigateBack();
      } else {
        app.showToast(c.msg)
      }
    })
  }


})